package com.ruoyi.app.api;

import com.ruoyi.common.constant.BusinessConst;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.appvo.TreeData;
import com.ruoyi.system.domain.wx.WxIcon;
import com.ruoyi.system.domain.wx.pay.WxPayType;
import com.ruoyi.system.service.wx.IWxIconService;
import com.ruoyi.system.service.wx.pay.IWxPayTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

/**
 * 微信支出api
 *
 * @author mic
 * @date 2023年8月17日11:24:50
 */
@RestController
@RequestMapping("/wx/type")
public class WxPayTypeAPI extends BaseController {
    @Autowired
    private IWxPayTypeService typeService;
    @Autowired
    private IWxIconService iconService;

    /**
     * 用户下的所有类型 内置和自建
     */
    @GetMapping("/allTypeByTree")
    public AjaxResult allTypeByTree(HttpServletRequest request) {
        // 自建
        WxPayType payType = new WxPayType();
        payType.setCreateBy((String) request.getAttribute("userId"));
        List<WxPayType> typelist = typeService.selectWxPayTypeList(payType);

        // 内置
        payType.setCreateBy(null);
        payType.setIsSys(BusinessConst.YesOrNo.YES);
        List<WxPayType> types = typeService.selectWxPayTypeList(payType);

        // 合并 去重
        typelist.addAll(types);
        List<WxPayType> list = typelist.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(Comparator.comparing(WxPayType::getTypeId))), ArrayList::new));

        // 封装成树形实体结构
        List<TreeData> treeList = new ArrayList<>();
        if (list.size() > 0) {
            list = list.stream().sorted(Comparator.comparing(WxPayType::getTypeId)).collect(Collectors.toList());
            List<WxPayType> collect = list.stream().distinct().collect(Collectors.toList());
            for (int i = 0; i < collect.size(); i++) {
                TreeData treeData = new TreeData();
                treeData.setCode(collect.get(i).getTypeId());
                treeData.setLabel(collect.get(i).getTypeName());
                treeData.setParentCode(collect.get(i).getParentId());
                if (StringUtils.isNotBlank(collect.get(i).getIcon())){
                    WxIcon wxIcon = iconService.selectwxIconByIconId(collect.get(i).getIcon());
                    if (wxIcon != null){
                        treeData.setIcon(wxIcon.getUrl());
                    }
                }
                treeList.add(treeData);
            }
        }

        // 转换成树形
        List<TreeData> trees = treeList.stream()
                // 筛选出父节点
                .filter(t -> BusinessConst.YesOrNo.NO.equals(t.getParentCode()))
                // 设置父节点的子节点列表
                .map(item -> {
                    item.setChilds(typeService.streamGetChildren(item, treeList));
                    return item;
                })
                .collect(Collectors.toList());


        return AjaxResult.success(trees);
    }
}
